﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using Zero.DataLayer.Donnees;
using Zero.DataLayer.Donnees.Tarifs;
using Zero.POCO;
using Zero.POCO.Compagne;
using Zero.POCO.Donnees;
using AudienceJournal = Zero.POCO.Audiences.AudienceJournal;
using Vague = Zero.POCO.Donnees.Vague;

namespace Zero.DataLayer.MAP
{
    public static class Map
    {
        public static void initSupport(Session uow)
        {
            #region init support
            var SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 2;
            SuppPresse.Libelle = "Algerie News";
            SuppPresse.Code = 2;
            SuppPresse.Save();


            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 25;
            SuppPresse.Libelle = "Transaction d Algérie";
            SuppPresse.Code = 25;
            SuppPresse.Save();


            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 9;
            SuppPresse.Libelle = "El Khabar";
            SuppPresse.Code = 9;
            SuppPresse.Save();


            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 19;
            SuppPresse.Libelle = "le Quotidien d Oran";
            SuppPresse.Code = 19;
            SuppPresse.Save();

            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 16;
            SuppPresse.Libelle = "la Nouvelle République";
            SuppPresse.Code = 16;
            SuppPresse.Save();

            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 12;
            SuppPresse.Libelle = "El Yaoum";
            SuppPresse.Code = 12;
            SuppPresse.Save();

            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 4;
            SuppPresse.Libelle = "Dajzair News (Ar)";
            SuppPresse.Code = 4;
            SuppPresse.Save();

            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 20;
            SuppPresse.Libelle = "le Soir d Algérie";
            SuppPresse.Code = 20;
            SuppPresse.Save();



            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 10;
            SuppPresse.Libelle = "El Moudjahid";
            SuppPresse.Code = 10;
            SuppPresse.Save();


            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 1;
            SuppPresse.Libelle = "Akher Saa";
            SuppPresse.Code = 1;
            SuppPresse.Save();



            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 24;
            SuppPresse.Libelle = "Midi Libre";
            SuppPresse.Code = 24;
            SuppPresse.Save();


            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid =6;
            SuppPresse.Libelle = "l Echo d Oran";
            SuppPresse.Code = 6;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 22;
            SuppPresse.Libelle = "l Expression";
            SuppPresse.Code = 22;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 3;
            SuppPresse.Libelle = "Competition";
            SuppPresse.Code = 3;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 18;
            SuppPresse.Libelle = "le Maghreb";
            SuppPresse.Code = 18;
            SuppPresse.Save();



            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 7;
            SuppPresse.Libelle = "El Haddef";
            SuppPresse.Code = 7;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 14;
            SuppPresse.Libelle = "Horizons";
            SuppPresse.Code = 14;
            SuppPresse.Save();



            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 13;
            SuppPresse.Libelle = "Ennahar al-Jadid";
            SuppPresse.Code = 13;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 17;
            SuppPresse.Libelle = "le Jeune Indépendant";
            SuppPresse.Code = 17;
            SuppPresse.Save();



            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 11;
            SuppPresse.Libelle = "El Watan";
            SuppPresse.Code = 11;
            SuppPresse.Save();


            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 23;
            SuppPresse.Libelle = "Liberté";
            SuppPresse.Code = 23;
            SuppPresse.Save();



            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 26;
            SuppPresse.Libelle = "Waqt el-Djazair";
            SuppPresse.Code = 26;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 21;
            SuppPresse.Libelle = "le Temps";
            SuppPresse.Code = 21;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 15;
            SuppPresse.Libelle = "Info Soir";
            SuppPresse.Code = 15;
            SuppPresse.Save();


            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 5;
            SuppPresse.Libelle = "Echorouq";
            SuppPresse.Code = 5;
            SuppPresse.Save();




            SuppPresse = new XpoSupportPresse(uow);
            SuppPresse.Oid = 8;
            SuppPresse.Libelle = "le Buteur";
            SuppPresse.Code = 8;
            SuppPresse.Save();







            var Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 31;
            Supptv.Libelle = "TV5";
            Supptv.Code = 31;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 25;
            Supptv.Libelle = "Al ARABIYA";
            Supptv.Code =25 ;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 34;
            Supptv.Libelle = "Autres Chaine Francophones";
            Supptv.Code = 34;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 4;
            Supptv.Libelle = "Tamazight Algérie TV4";
            Supptv.Code = 4;
            Supptv.Save();






            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 29;
            Supptv.Libelle = "M6";
            Supptv.Code = 29;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 99;
            Supptv.Libelle = "LA 5e";
            Supptv.Code = 99;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 21;
            Supptv.Libelle = "2M (Maroc)";
            Supptv.Code = 21;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 98;
            Supptv.Libelle = "Al ABASSIYA";
            Supptv.Code = 99;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 23;
            Supptv.Libelle = "Melody Aflam";
            Supptv.Code = 23;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 3;
            Supptv.Libelle = "A3C";
            Supptv.Code = 3;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 18;
            Supptv.Libelle = "Medi 1 sat";
            Supptv.Code = 18;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 10;
            Supptv.Libelle = "MBC Action";
            Supptv.Code = 10;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 9;
            Supptv.Libelle = "MBC 4";
            Supptv.Code = 9;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 11;
            Supptv.Libelle = "MBC Max";
            Supptv.Code = 11;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 97;
            Supptv.Libelle = "NUMIDIA NEWS";
            Supptv.Code = 99;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 8;
            Supptv.Libelle = "MBC 3";
            Supptv.Code = 8;
            Supptv.Save();



            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 0;
            Supptv.Libelle = "AL MASRIA";
            Supptv.Code = 0;
            Supptv.Save();





            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 24;
            Supptv.Libelle = "Rotana Cinéma";
            Supptv.Code = 24;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 13;
            Supptv.Libelle = "Nessma TV";
            Supptv.Code = 13;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 21;
            Supptv.Libelle = "Echorouk TV";
            Supptv.Code = 21;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 33;
            Supptv.Libelle = "Autres Chaine Arabophones";
            Supptv.Code = 33;
            Supptv.Save();



            Supptv = new XpoSupportTV(uow);
            Supptv.Oid =28;
            Supptv.Libelle = "FRANCE 3";
            Supptv.Code = 28;
            Supptv.Save();

            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 96;
            Supptv.Libelle = "BEUR TV";
            Supptv.Code = 99;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 12;
            Supptv.Libelle = "MBC Drama";
            Supptv.Code = 12;

            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid =  16;
            Supptv.Libelle = "Al JAZEERA SPORT (Payante )";
            Supptv.Code = 16;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 95;
            Supptv.Libelle = "TV7 (Tunisie)";
            Supptv.Code = 99;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 30;
            Supptv.Libelle = "CANAL+";
            Supptv.Code = 30;
            Supptv.Save();

            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 94;
            Supptv.Libelle = "ARTE";
            Supptv.Code =99;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 32;
            Supptv.Libelle = "FRANCE 24";
            Supptv.Code = 32;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 7;
            Supptv.Libelle = "MBC 2";
            Supptv.Code =7;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 26;
            Supptv.Libelle = "TF1";
            Supptv.Code = 26;
            Supptv.Save();



            Supptv = new XpoSupportTV(uow);
            Supptv.Oid =93;
            Supptv.Libelle = "MBC Maser";
            Supptv.Code = 99;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 14;
            Supptv.Libelle = "Al JAZEERA NEWS";
            Supptv.Code = 14;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 22;
            Supptv.Libelle = "El Magharibia TV";
            Supptv.Code = 22;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid =92;
            Supptv.Libelle = "El Magharibia TV";
            Supptv.Code = 22;
            Supptv.Save();



            Supptv = new XpoSupportTV(uow);
            Supptv.Oid=91;
            Supptv.Libelle = "Al JAZEERA SPORT";
            Supptv.Code = 99;
            Supptv.Save();



            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 1;
            Supptv.Libelle = "ENTV";
            Supptv.Code = 1;
            Supptv.Save();



            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 15;
            Supptv.Libelle = "Al JAZEERA SPORT ( Gratuit )";
            Supptv.Code = 15;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 6;
            Supptv.Libelle = "MBC 1";
            Supptv.Code = 6;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 27;
            Supptv.Libelle = "FRANCE 2";
            Supptv.Code = 27;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 91;
            Supptv.Libelle = "Dubai TV";
            Supptv.Code = 99;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 5;
            Supptv.Libelle = "TV Coran Algérie TV5";
            Supptv.Code = 5;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 19;
            Supptv.Libelle = "AN Nahar TV";
            Supptv.Code = 19;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 17;
            Supptv.Libelle = "FOX Movies";
            Supptv.Code = 17;
            Supptv.Save();




            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 90;
            Supptv.Libelle = "Abu Dhabi";
            Supptv.Code = 99;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 2;
            Supptv.Libelle = "Canal Algérie";
            Supptv.Code = 2;
            Supptv.Save();


            Supptv = new XpoSupportTV(uow);
            Supptv.Oid = 20;
            Supptv.Libelle = "AL JAZAIRIA";
            Supptv.Code = 20;
            Supptv.Save();


                          
                var supprd = new XpoSupportRadio(uow);
            supprd.Oid = 107;
            supprd.Libelle = "Radio Cirta";
            supprd.Code = 107;
            supprd.Save();


            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 101;
            supprd.Libelle = "Chaine 1";
            supprd.Code = 101;
            supprd.Save();


            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 102;
            supprd.Libelle = "Chaine 2";
            supprd.Code = 102;
            supprd.Save();


            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 104;
            supprd.Libelle = "Jil FM";
            supprd.Code = 104;
            supprd.Save();

            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 103;
            supprd.Libelle = "Chaine 3";
            supprd.Code = 103;
            supprd.Save();

            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 110;
            supprd.Libelle = "Autres Radio";
            supprd.Code = 110;
            supprd.Save();


            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 109;
            supprd.Libelle = "Radio El Bahia";
            supprd.Code = 109;
            supprd.Save();


            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 108;
            supprd.Libelle = "Radio El Hidhab";
            supprd.Code = 108;
            supprd.Save();

            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 105;
            supprd.Libelle = "Radio Algérie International";
            supprd.Code = 105;
            supprd.Save();

            supprd = new XpoSupportRadio(uow);
            supprd.Oid = 106;
            supprd.Libelle = "Radio El Bahdja";
            supprd.Code = 106;
            supprd.Save();
            #endregion 
        }

        public static  Donnees.Signalitique MapSignalitique(ISignalitique signlitique, UnitOfWork uow)
        {
            var sig = new Donnees.Signalitique(uow);
            sig.Oid = signlitique.Id;
            sig.Age = signlitique.Age;
            sig.AppareilPhotoNumerique = signlitique.AppareilPhotoNumerique;
            sig.Aspirateur = signlitique.Aspirateur;
            sig.CSE = (signlitique.CSE);
            sig.CSP = signlitique.CSP;
            sig.Camescope = signlitique.Camescope;
            sig.Chainestereo = signlitique.Chainestereo;
            sig.Climatiseur = signlitique.Climatiseur;
            sig.Congelateur = signlitique.Congelateur;
            sig.Console = signlitique.Console;
            sig.Cuisiniere = signlitique.Cuisiniere;
            sig.Decodeur = signlitique.Decodeur;
            sig.Fourmicroonde=signlitique.Fourmicroonde;
            sig.Genre = signlitique.Genre;
            sig.Habitat = signlitique.Habitat;
            sig.Jour = signlitique.Jour;
            sig.LaveLinge = signlitique.LaveLinge;
            sig.LaveVaissele = signlitique.LaveVaissele;
            sig.LecteurCD = signlitique.LecteurCD;
            sig.LecteurDVD = signlitique.LecteurDVD;
            sig.Lecteurradiocassette = signlitique.Lecteurradiocassette;
            sig.Magnetoscope = signlitique.Magnetoscope;
            sig.Mobile = signlitique.Mobile;
            sig.Mois = signlitique.Mois;
            sig.NbPiece = signlitique.NbPiece;
            sig.NbVoiture = signlitique.NbVoiture;
            sig.Niveau = sig.Niveau;
            sig.PCConnexion = sig.Pcsansconnexion;
            sig.Pcsansconnexion = sig.Pcsansconnexion;
            sig.Poid = signlitique.Poid;
            sig.Refrigirateur = signlitique.Refrigirateur;
            sig.RevenueTotalFoyer = signlitique.RevenueTotalFoyer;
            sig.SecheLinge = signlitique.SecheLinge;
            sig.Situationmatrimoniale = signlitique.Situationmatrimoniale;
            sig.Statutfamilial = signlitique.Statutfamilial;
            sig.TailleFoyer = signlitique.TailleFoyer;
            sig.Telephone = signlitique.Telephone;
            sig.Televisionencouleur = signlitique.Televisionencouleur;
            sig.TrancheAge = signlitique.TrancheAge;
            sig.TypeHabitat = signlitique.TypeHabitat;
            sig.Ville = signlitique.Ville;
            sig.Individu = MapIndividu(signlitique.Individu, uow);

            return sig;
        }

        public static  Donnees.Vague MapVague(Vague vague, UnitOfWork uow, int nBInstance=0,int iteration=1,Zero.DataLayer.Donnees.Vague vagueBD=null)
        {
            Zero.DataLayer.Donnees.Vague vag;
            if (vagueBD == null)
            {
                vag = new Donnees.Vague(uow);
                vag.Annee = vague.Annee;
                vag.Libelle = vague.Libelle;
                vag.Mois = vague.Mois;
                vag.Numero = vague.Numero;
                vag.Rang = vague.Rang;
                vag.Ecran = vague.Ecran;
                vag.NbJour = vague.NbJour;
                vag.DateDebut = vague.DateDebut;

                vag.NombreIndividus = vague.NombreIndividus;
                vag.NombreCalsse_A = vague.NombreCalsse_A;
                vag.NombreCalsse_B = vague.NombreCalsse_B;
                vag.NombreCalsse_C = vague.NombreCalsse_C;
                vag.NombreCalsse_D = vague.NombreCalsse_D;
                vag.NombreAlger = vague.NombreAlger;
                vag.NombreAutreInactif = vague.NombreAutreInactif;
                vag.NombreCadre = vague.NombreCadre;
                vag.NombreCadreMoyen = vague.NombreCadreMoyen;
                vag.NombreChomeur = vague.NombreChomeur;
                vag.NombreConstantine = vague.NombreConstantine;
                vag.NombreSansInstructionLireEtEcrire = vague.NombreSansInstructionLireEtEcrire;
                vag.NombreEmployee = vague.NombreEmployee;
                vag.NombreEntre_12_25 = vague.NombreEntre_12_25;
                vag.NombreEntre_25_55 = vague.NombreEntre_25_55;
                vag.NombreFemme = vague.NombreFemme;
                vag.NombreFemmeAuFoyer = vague.NombreFemmeAuFoyer;
                vag.NombreHautFonctionnaire = vague.NombreHautFonctionnaire;
                vag.NombreHommes = vague.NombreHomme;
                vag.NombreProfessionLiberal = vague.NombreProfessionLiberal;
                vag.NombreMoins_12 = vague.NombreMoins_12;
                vag.NombreMoyen = vague.NombreMoyen;
                vag.NombreOran = vague.NombreOran;
                vag.NombrePensionne = vague.NombrePensionne;
                vag.NombrePetitPatron = vague.NombrePetitPatron;
                vag.NombrePlus_55 = vague.NombrePlus_55;
                vag.NombrePrimaire = vague.NombrePrimaire;
                vag.NombreRetraite = vague.NombreRetraite;
                vag.NombreSansInstruction = vague.NombreSansInstruction;
                vag.NombreSetif = vague.NombreSetif;
                vag.NombreSecondaire = vague.NombreSecondaire;
                vag.NombreUniversitaire = vague.NombreUniversitaire;
                
                
                
                vag.Oid = 1;
            }
            else vag = vagueBD;
            var i = 0;
            var listeSuportPresse = new XPCollection<XpoSupportPresse>(uow);
            var listeSuportTv = new XPCollection<XpoSupportTV>(uow);
            var listeSuportRadio = new XPCollection<XpoSupportRadio>(uow);
            foreach (var signalitique in vague.Signalitique.Skip((iteration-1)*20).Take(nBInstance))
            {//TODO: rappel jai essayer de deviser les sauvegarde en 200 a la fois
                i++;
                var sig = MapSignalitique(signalitique, uow);
                foreach (var AudPresse in signalitique.AudienceJournals)
                {
                    var aud = MapAudJournal(AudPresse,uow, listeSuportPresse);
                    sig.AudienceJournals.Add(aud);
                }

                foreach (var AudTV in signalitique.AudienceTVs)
                {
                    var aud = MapAudTV(AudTV, uow,listeSuportTv);
                    sig.AudienceTVs.Add(aud);
                }

                foreach (var AudRad in signalitique.AudienceRadios)
                {
                    var aud = MapAudRD(AudRad, uow,listeSuportRadio);
                    sig.AudienceRadios.Add(aud);
                }


                vag.Signalitiques.Add(sig);
            }
            return vag;
        }

        public static Donnees.AudienceJournal MapAudJournal(AudienceJournal Adj, UnitOfWork uow, XPCollection<XpoSupportPresse> supports)
        {
            var audience = new Donnees.AudienceJournal(uow);
            audience.NombreOccurence = Adj.NombreOccurence;
            audience.NumJournal = Adj.NumJournal;
            audience.Date = Adj.Date;
            audience.Oid = new int();
            audience.FiltrePriotaire = Adj.FiltrePriotaire;
            audience.Filtresecondaire = Adj.Filtresecondaire;
            audience.Age = Adj.Age;
            audience.Ville = Adj.Ville;
            
            audience.CSE = Adj.CSE;
            audience.CSP = Adj.CSP;
            audience.Equipement = Adj.Equipement;
            audience.Habitat = Adj.Habitat;
            audience.NbPiece = Adj.NbPiece;
            audience.NbVoiture = Adj.NbVoiture;
            audience.Niveau = Adj.Niveau;
            audience.RevenueTotalFoyer = Adj.RevenueTotalFoyer;
            audience.Situationmatrimoniale = Adj.Situationmatrimoniale;
            audience.Statutfamilial = Adj.Statutfamilial;
            audience.TailleFoyer = Adj.TailleFoyer;
            audience.TypeHabitation = Adj.TypeHabitat;
            audience.Genre = Adj.Genre;

            audience.NumeroJourSemaine = Adj.NumeroJourSemaine;

            var support = supports.First(e => e.Code==Adj.NumJournal);

            audience.SupportPresse = support;
            return audience;
        }

        public static AudienceTV MapAudTV(Zero.POCO.Audiences.AudienceTV Adj, UnitOfWork uow, XPCollection<XpoSupportTV> supports)
        {
            var audience = new Donnees.AudienceTV(uow);

            audience.Oid = new int();
            audience.NumeroQuertdheure = Adj.NumeroQuertdheure;
            audience.NumeroJour = Adj.NumeroJour;
            audience.Minute = Adj.NumeroMinute;
            audience.Date = Adj.Date;
            audience.FiltrePriotaire = Adj.FiltrePriotaire;
            audience.Filtresecondaire = Adj.Filtresecondaire;
            audience.Age = Adj.Age;
            audience.Poid = Adj.Poid;
            audience.Ville = Adj.Ville;

            audience.Genre = Adj.Genre;

            audience.CSE = Adj.CSE;
            audience.CSP = Adj.CSP;
            audience.Equipement = Adj.Equipement;
            audience.Habitat = Adj.Habitat;
            audience.NbPiece = Adj.NbPiece;
            audience.NbVoiture = Adj.NbVoiture;
            audience.Niveau = Adj.Niveau;
            audience.RevenueTotalFoyer = Adj.RevenueTotalFoyer;
            audience.Situationmatrimoniale = Adj.Situationmatrimoniale;
            audience.Statutfamilial = Adj.Statutfamilial;
            audience.TailleFoyer = Adj.TailleFoyer;
            audience.TypeHabitation = Adj.TypeHabitat;
            audience.NumeroJourSemaine = Adj.NumeroJourSemaine;
            var support = supports.First(e => e.Code == Adj.SupportTV.Code);
            
            audience.SupportTV = support;
          
            return audience;
        }

        public static AudienceRadio MapAudRD(POCO.Audiences.AudienceRadio Adj, UnitOfWork uow, XPCollection<XpoSupportRadio> supports)
        {
            var audience = new Donnees.AudienceRadio(uow);

            audience.Oid = new int();
            audience.QuartHeure = Adj.QuartHeure;
            audience.NumeroJour = Adj.NumeroJour;
            audience.Minute = Adj.NumeroMinute;
            audience.Date = Adj.Date;
            audience.FiltrePriotaire = Adj.FiltrePriotaire;
            audience.Filtresecondaire = Adj.Filtresecondaire;
            audience.Age = Adj.Age;
            audience.Ville = Adj.Ville;
            var support = supports.First(e => e.Code == Adj.SupportRadio.Code); 
            audience.SupportRadio = support;

            audience.Genre = Adj.Genre;
            audience.CSE = Adj.CSE;
            audience.CSP = Adj.CSP;
            audience.Equipement = Adj.Equipement;
            audience.Habitat = Adj.Habitat;
            audience.NbPiece = Adj.NbPiece;
            audience.NbVoiture = Adj.NbVoiture;
            audience.Niveau = Adj.Niveau;
            audience.RevenueTotalFoyer = Adj.RevenueTotalFoyer;
            audience.Situationmatrimoniale = Adj.Situationmatrimoniale;
            audience.Statutfamilial = Adj.Statutfamilial;
            audience.TailleFoyer = Adj.TailleFoyer;
            audience.TypeHabitation = Adj.TypeHabitat;
            audience.NumeroJourSemaine = Adj.NumeroJourSemaine;

            audience.Poid = Adj.Poid;

            return audience;
        }

        public static Donnees.Individu MapIndividu(Zero.POCO.Donnees.Individu indiv, UnitOfWork uow)
        {
            Donnees.Individu XpoIndividu;
            if (indiv.id == default(Guid))
            {
                XpoIndividu = new Donnees.Individu(uow);
               XpoIndividu.Oid=new Guid();
            }
            else
            {
                XpoIndividu = uow.GetObjectByKey<Donnees.Individu>(indiv.id);
                XpoIndividu.Oid = indiv.id;
            }

             
                XpoIndividu.Sexe = indiv.Sexe;
                XpoIndividu.IdQuest = indiv.IdQuest;

            return XpoIndividu;
        }

        public static XpoAnnonceur MapAnnonceur(Annonceur annonceur,UnitOfWork uow,XpoAnnonceur annonceurModifie=null)
        {
            XpoAnnonceur xpoannonceur;
            if (annonceurModifie == null)
            {
                if (annonceur.id == default(Guid))
                    xpoannonceur = new XpoAnnonceur(uow)
                    {
                        Name = annonceur.Name,
                        Oid = annonceur.id,
                        Domaine = annonceur.Domaine
                    };
                else
                    xpoannonceur = uow.GetObjectByKey<XpoAnnonceur>(annonceur.id);
                foreach (var marque in annonceur.Marques)
                {
                    xpoannonceur.Marques.Add(MapMarque(marque, uow));
                }
                return xpoannonceur;
            }

            annonceurModifie.Name = annonceur.Name;
            annonceurModifie.Domaine = annonceur.Domaine;
            
            foreach (var marque in annonceur.Marques)
            {
                annonceurModifie.Marques.Add(MapMarque(marque, uow));
            }
            return annonceurModifie;
        }


        public static XpoMarque MapMarque(Marque marque,UnitOfWork uow)
        {
            XpoMarque mar;
            if (marque.id == default(Guid))
                mar = new XpoMarque(uow) {Oid = marque.id, Name = marque.Name, Type = marque.Type};
            else
                mar = uow.GetObjectByKey<XpoMarque>(marque.id);
            return mar;
        }
        public static object MapCompaign(Compaign entity, UnitOfWork uow)
        {
            XpoCompaign compagne = null;
            if(entity.id==default(Guid))
            {
                compagne = new XpoCompaign(uow);
                compagne.Annonceur = MapAnnonceur(entity.Annonceur,uow);compagne.Marque = MapMarque(entity.Marque,uow);
                compagne.Oid = Guid.NewGuid();
                compagne.Filter = MapFiltre(entity.Filtre,uow);
                compagne.Vague = uow.GetObjectByKey<Donnees.Vague>(entity.Vague.id);
                compagne.Nom = entity.Name;
            }
            /*foreach (var planPresse in entity.PlanPresses)
            {
                compagne.PlanPresses.Add(MapPlanPresse(planPresse,uow));
            }



            foreach (var planTv in entity.PlanTvs)
            {
                compagne.PlanTvs.Add(MapplanTv(planTv, uow));
            }


            foreach (var planRadio in entity.PlanRadios)
            {
                compagne.PlanRadios.Add(MapplanRadio(planRadio, uow));
            }*/
            return compagne;
        }








        public static XpoTarifPr MapTarifPr(TarifPr entity, UnitOfWork uow)
        {

            XpoTarifPr tarifPr;
            if (entity.id != default(Guid))
            {
                tarifPr = uow.GetObjectByKey<XpoTarifPr>(entity.id);
            }
            else
            {

                tarifPr = new XpoTarifPr(uow);
                tarifPr.Oid = Guid.NewGuid();
            }

            tarifPr.Nom = entity.nom;
            tarifPr.Annee = entity.annee;
            tarifPr.Autre = entity.autre;
            tarifPr.Mois = entity.Mois;
                
               
            
            if (entity.supportPrs!=null)
                foreach (var Sup in entity.supportPrs)
            {
                var supTemp=uow.GetObjectByKey<XpoSupportPresse>(entity.id);
                if(supTemp!=null)tarifPr.SupportPresses.Add(supTemp);
            }

            if (entity.LignesTarifPrs!=null)
            foreach (var lig in entity.LignesTarifPrs)
            {

                var templig = MapLigneTarifPresse(lig,uow);
                if(templig!=null)
                    tarifPr.LigneTarifPrs.Add(templig);
            }
            List<String> listeSupport = entity.supportPrs;
            if (listeSupport != null)
                foreach (var support in listeSupport)
                {
                    var xposupport = uow.FindObject<XpoSupportPresse>(new BinaryOperator("Libelle", support));
                    if (xposupport != null)
                        tarifPr.SupportPresses.Add(xposupport);
                }
            return tarifPr;
        }

        private static XpoLigneTarifPr MapLigneTarifPresse(LignesTarifPr lig,UnitOfWork uow)
        {
            XpoLigneTarifPr ligneTarifPr;
            
            if (lig.id != default(Guid))
            {
                ligneTarifPr = uow.GetObjectByKey<XpoLigneTarifPr>(lig.id);

            }
            else
            {
                ligneTarifPr = new XpoLigneTarifPr(uow);
                ligneTarifPr.Oid = Guid.NewGuid();
            }


            ligneTarifPr.Format = lig.Format;
            ligneTarifPr.JourSemaine = lig.JourSemaine;
            ligneTarifPr.Prix = lig.Prix;

            return ligneTarifPr;

        }






        public static XpoTarifRd MapTarifRd(TarifRd entity, UnitOfWork uow)
        {
            XpoTarifRd tarifRd;
            if (entity.id != default(Guid))
            {
                tarifRd = uow.GetObjectByKey<XpoTarifRd>(entity.id);
            }
            else
            {
                tarifRd = new XpoTarifRd(uow);
                tarifRd.Oid = Guid.NewGuid();
            }

            tarifRd.Nom = entity.nom;
            tarifRd.Annee = entity.annee;
            tarifRd.Autre = entity.autre;
            tarifRd.Mois = entity.Mois;
                
            
            if (entity.supportRds!=null)
            foreach (var Sup in entity.supportRds)
            {
                var supTemp = uow.GetObjectByKey<XpoSupportRadio>(entity.id);
                if (supTemp != null)
                    tarifRd.SupportRadios.Add(supTemp);
            }

            if (entity.LignesTarifRds!=null)
            foreach (var lig in entity.LignesTarifRds)
            {
                var templig = MapLigneTarifRadio(lig, uow);
                if (templig != null)
                    tarifRd.LigneTarifRds.Add(templig);
            }
            List<String> listeSupport = entity.supportRds;
            if (listeSupport != null)
                foreach (var support in listeSupport)
                {
                    var xposupport = uow.FindObject<XpoSupportRadio>(new BinaryOperator("Libelle", support));
                    if (xposupport != null)
                        tarifRd.SupportRadios.Add(xposupport);

                }
            return tarifRd;
        }

        private static XpoLigneTarifRd MapLigneTarifRadio(LignesTarifRd lig, UnitOfWork uow)
        {


            XpoLigneTarifRd ligneTarifRd;
            if (lig.id != default(Guid))
            {
                ligneTarifRd = uow.GetObjectByKey<XpoLigneTarifRd>(lig.id);
            }
            else
            {
                ligneTarifRd = new XpoLigneTarifRd(uow);
                ligneTarifRd.Oid = Guid.NewGuid();
            }



            ligneTarifRd.Duree = lig.duree;
            ligneTarifRd.JourSemaine = lig.JourSemaine;
            ligneTarifRd.Prix = lig.Prix;
            ligneTarifRd.Debut = lig.Debut;
            ligneTarifRd.Fin = lig.Fin;
            return ligneTarifRd;
        }












        public static XpoTarifTv MapTarifTv(TarifTv entity, UnitOfWork uow)
        {

            XpoTarifTv tarifTv;
            if (entity.id != default(Guid))
            {
                tarifTv = uow.GetObjectByKey<XpoTarifTv>(entity.id);
                tarifTv.Oid = entity.id;
            }
            else
            {
                tarifTv= new XpoTarifTv(uow);
                tarifTv.Oid = Guid.NewGuid();
            }


            tarifTv.Nom = entity.nom;
            tarifTv.Annee = entity.annee;
            tarifTv.Autre = entity.autre;
            tarifTv.Mois = entity.Mois;
                

            /*
            if (entity.supportTvs!=null)
            foreach (var Sup in entity.supportTvs)
            {
                var supTemp = uow.GetObjectByKey<XpoSupportTV>(Sup.);
                if (supTemp != null)
                    tarifTv.SupportTvs.Add(supTemp);
            }*/

            if (entity.LignesTarifTvs!=null)
            foreach (var lig in entity.LignesTarifTvs)
            {
                var templig = MapLigneTarifTV(lig, uow);
                if (templig != null)
                    tarifTv.LigneTarifTvs.Add(templig);
            }

            List<String> listeSupport = entity.supportTvs;

            foreach (var support in listeSupport)
            {
               var xposupport= uow.FindObject<XpoSupportTV>(new BinaryOperator("Libelle", support));
                if (xposupport!=null)
                    tarifTv.SupportTvs.Add(xposupport);
                
            }
            return tarifTv;
        }

        private static XpoLigneTarifTv MapLigneTarifTV(LignesTarifTv lig, UnitOfWork uow)
        {
            XpoLigneTarifTv ligneTarifTv;
            if (lig.id != default(Guid))
            {
                ligneTarifTv = uow.GetObjectByKey<XpoLigneTarifTv>(lig.id);
            }
            else
            {
                ligneTarifTv = new XpoLigneTarifTv(uow);
                ligneTarifTv.Oid=Guid.NewGuid();
            }
            
            ligneTarifTv.Duree = lig.duree;
            ligneTarifTv.JourSemaine = lig.JourSemaine;
            ligneTarifTv.Prix = lig.Prix;
            ligneTarifTv.Debut = lig.Debut;
            ligneTarifTv.Fin = lig.Fin;
            return ligneTarifTv;
        }






        public static XpoFilter MapFiltre(Filter filtre, UnitOfWork uow)
        {
            XpoFilter xpofilter;
            if(filtre.id==default(Guid))

            xpofilter = new XpoFilter(uow)
            {
                ageMin = filtre.ageMin,
                ageMax = filtre.ageMax,
                Oid = Guid.NewGuid(),
                CSPs = filtre.CSPs,
                CSEs = filtre.CSEs,
                Nom = filtre.Nom,
                TypeHabitation = filtre.TypeHabitations,
                Equipement = filtre.Equipements,
                Genre = filtre.Genres,
                Habitat = filtre.Habitats,
                NbChambre = filtre.NbChambres,
                NbVoiture = filtre.NbVoitures,
                NiveauInstruction = filtre.NiveauxInstructions,
                RevenuTotalFoyer = filtre.RevenueTotalFoyers,
                SituationMatrimonial = filtre.SituationMatrimonials,
                StatutFamiliale = filtre.StatutFamiliales,
                TailleFoyer = filtre.TailleFoyers,
                Representativite = filtre.GetRepresentativite()

            };
            else
            {
                xpofilter = uow.GetObjectByKey<XpoFilter>(filtre.id);
                {
                    xpofilter.ageMin = filtre.ageMin;
                    xpofilter.ageMax = filtre.ageMax;
                    xpofilter.CSPs = filtre.CSPs;
                    xpofilter.CSEs = filtre.CSEs;
                    xpofilter.Nom = filtre.Nom;
                    xpofilter.TypeHabitation = filtre.TypeHabitations;
                    xpofilter.Equipement = filtre.Equipements;
                    xpofilter.Genre = filtre.Genres;
                    xpofilter.Habitat = filtre.Habitats;
                    xpofilter.NbChambre = filtre.NbChambres;
                    xpofilter.NbVoiture = filtre.NbVoitures;
                    xpofilter.NiveauInstruction = filtre.NiveauxInstructions;
                    xpofilter.RevenuTotalFoyer = filtre.RevenueTotalFoyers;
                    xpofilter.SituationMatrimonial = filtre.SituationMatrimonials;
                    xpofilter.StatutFamiliale = filtre.StatutFamiliales;
                    xpofilter.TailleFoyer = filtre.TailleFoyers;
                    xpofilter.Representativite = filtre.GetRepresentativite();

                };
            }
            return xpofilter;


        }

        public static XpoPlanRadio MapplanRadio(PlanRadio planRadio, UnitOfWork uow)
        {
            XpoPlanRadio xpoPlanRadio;

            if (planRadio.id == default(Guid))
            {
                xpoPlanRadio = new XpoPlanRadio(uow);
                xpoPlanRadio.Oid = Guid.NewGuid();
            }
            else
            {
                xpoPlanRadio = uow.GetObjectByKey<XpoPlanRadio>(planRadio.id);
            }
            xpoPlanRadio.Name = planRadio.Name;

            xpoPlanRadio.NombreSig = planRadio.NombreSignalitique;


            if (planRadio.InsertionRadios != null)
            {
                uow.Delete(xpoPlanRadio.XpoInsertionRadios);
                foreach (var insertionRadio in planRadio.InsertionRadios)
            {
                xpoPlanRadio.XpoInsertionRadios.Add(MapInsertionRadio(insertionRadio, uow));
            }}

            if (planRadio.ListeMapSupportTarifs != null)
            {
                uow.Delete(xpoPlanRadio.XpoMapSupportTarifRds);
                foreach (var mapSupportTarifs in planRadio.ListeMapSupportTarifs)
                {
                    xpoPlanRadio.XpoMapSupportTarifRds.Add(MapmapTarifRd(mapSupportTarifs, uow));
                }
            }

            return xpoPlanRadio;
        }

        private static XpoMapSupportTarifRd MapmapTarifRd(MapSupportTarif mapSupportTarifs, UnitOfWork uow)
        {
            return new XpoMapSupportTarifRd()
            {
                Oid = mapSupportTarifs.id,
                TarifRd = MapTarifRd(mapSupportTarifs.TarifRd, uow),
                supportRadio = uow.GetObjectByKey<XpoSupportRadio>(mapSupportTarifs.SupportId)
            };
        }
        

        public static XpoPlanTv MapplanTv(PlanTV planTv, UnitOfWork uow)
        {

            XpoPlanTv xpoPlanTv;
            if (planTv.id == default(Guid))
            {
                xpoPlanTv = new XpoPlanTv(uow);
                xpoPlanTv.Oid = Guid.NewGuid();
            }
            else
            {
                xpoPlanTv=uow.GetObjectByKey<XpoPlanTv>(planTv.id);
            }

            xpoPlanTv.Name = planTv.Name;
            xpoPlanTv.NombreSig = planTv.NombreSignalitique;
            



            if (planTv.InsertionTvs != null)
            {
                uow.Delete(xpoPlanTv.XpoInsertionTVs);
                foreach (var insertiontv in planTv.InsertionTvs)
                {
                    xpoPlanTv.XpoInsertionTVs.Add(MapInsertionTV(insertiontv, uow));
                }
            }
            //TODO: optimiser lors d'une modification pour ne plus charger les insertions
            if (planTv.ListeMapSupportTarifs != null)
            {
                uow.Delete(xpoPlanTv.XpoMapSupportTarifTvs);
                foreach (var mapSupportTarifs in planTv.ListeMapSupportTarifs)
                {
                    xpoPlanTv.XpoMapSupportTarifTvs.Add(MapmapTarifTv(mapSupportTarifs, uow));
                }
            }

            return xpoPlanTv;
        }

        private static XpoMapSupportTarifTv MapmapTarifTv(MapSupportTarif mapSupportTarifs, UnitOfWork uow)
        {
            var temp = new XpoMapSupportTarifTv(uow);

            temp.Oid = Guid.NewGuid();
            XpoTarifTv mapTarifTv = MapTarifTv(mapSupportTarifs.TarifTv, uow);
            temp.TarifTv = mapTarifTv;
            temp.supportTV = uow.GetObjectByKey<XpoSupportTV>(mapSupportTarifs.SupportId);
            

            return temp;
        }
        public static XpoPlanPresse MapPlanPresse(PlanPresse planPresse, UnitOfWork uow)
        {
            XpoPlanPresse xpoPlanPresse;

            if (planPresse.id == default(Guid)){
                xpoPlanPresse = new XpoPlanPresse(uow);
                xpoPlanPresse.Oid = Guid.NewGuid();
            }
            else
            {
                xpoPlanPresse = uow.GetObjectByKey<XpoPlanPresse>(planPresse.id);
            }
            xpoPlanPresse.Name = planPresse.Name;
            xpoPlanPresse.NombreSig = planPresse.NombreSignalitique;
            



            if (planPresse.InsertionPresses != null)
            {
                uow.Delete(xpoPlanPresse.XpoInsertionPresses);
                foreach (var insertionPresse in planPresse.InsertionPresses)
                {
                    xpoPlanPresse.XpoInsertionPresses.Add(MapInsertionPresse(insertionPresse,uow));
                }
            }



            if (planPresse.ListeMapSupportTarifs != null)
            {
                uow.Delete(xpoPlanPresse.XpoMapSupportTarifPrs);
                foreach (var mapSupportTarifs in planPresse.ListeMapSupportTarifs)
                {
                    xpoPlanPresse.XpoMapSupportTarifPrs.Add(MapmapTarif(mapSupportTarifs,uow));
                }
            }

            return xpoPlanPresse;
        }

        private static XpoMapSupportTarifPr MapmapTarif(MapSupportTarif mapSupportTarifs,UnitOfWork uow)
        {
            return new XpoMapSupportTarifPr()
            {
                Oid = mapSupportTarifs.id,
                tarifPr = MapTarifPr(mapSupportTarifs.TarifPr,uow),
                supportPresse = uow.GetObjectByKey<XpoSupportPresse>(mapSupportTarifs.SupportId)
            };
        }

 


        private static XpoInsertionPresse MapInsertionPresse(InsertionPresse insertionPresse, UnitOfWork uow)
        {
            var xpoInsertionPresse = new XpoInsertionPresse(uow);
            xpoInsertionPresse.Date = insertionPresse.Date;
            xpoInsertionPresse.NombreFoisLus = insertionPresse.NombreFoisLus;
            xpoInsertionPresse.Oid = Guid.NewGuid();
            xpoInsertionPresse.SupportPresse = uow.GetObjectByKey<XpoSupportPresse>(insertionPresse.SupportPresse.id);
            if (insertionPresse.Signalitique != null)
            foreach (var signalitique in insertionPresse.Signalitique)
            {
                xpoInsertionPresse.Signalitiques.Add(MapSignalitique(signalitique, uow));
            }

            return xpoInsertionPresse;
        }


        private static XpoInsertionTV MapInsertionTV(InsertionTv insertiontv, UnitOfWork uow)
        {
            var xpoInsertionTv = new XpoInsertionTV(uow);
            xpoInsertionTv.Date = insertiontv.Date;
            xpoInsertionTv.NumeroQuartHeure = insertiontv.NumeroCinQMinute;
            xpoInsertionTv.Oid = Guid.NewGuid();
            xpoInsertionTv.SupportTV = uow.GetObjectByKey<XpoSupportTV>(insertiontv.SupportTv.id);
            if (insertiontv.Signalitique != null)
            foreach (var signalitique in insertiontv.Signalitique)
            {
                xpoInsertionTv.Signalitiques.Add(MapSignalitique(signalitique, uow));
            }

            return xpoInsertionTv;
        }


        private static XpoInsertionRadio MapInsertionRadio(InsertionRadio insertionRadio, UnitOfWork uow)
        {
            var xpoInsertionRadio = new XpoInsertionRadio(uow);
            xpoInsertionRadio.Date = insertionRadio.Date;
            xpoInsertionRadio.NumeroQuartHeure = insertionRadio.NumeroCinQMinute;
            xpoInsertionRadio.Oid = Guid.NewGuid();
            xpoInsertionRadio.SupportRadio = uow.GetObjectByKey<XpoSupportRadio>(insertionRadio.SupportRadio.id);
            if (insertionRadio.Signalitique != null)
            foreach (var signalitique in insertionRadio.Signalitique)
            {
                xpoInsertionRadio.Signalitiques.Add(MapSignalitique(signalitique, uow));
            }

            return xpoInsertionRadio; ;
        }
    }
}
